<?php


namespace app\hapi\controller;


use app\common\model\Lane;
use app\common\model\Parkinfo;
use app\common\model\Parklog;
use think\Controller;

class Parking extends ApiBase
{
    public function getparklist($param)
    {

        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        $cacheinfo=$this->getcacheinfo($param['token']);
        if($cacheinfo['parkid']){
            $map['id']=$cacheinfo['parkid'];
        }
        if (isset($query['title'])) {
            $map['parktitle'] =  array('like',"%".$query['title']."%");
        }

        $Parkinfo = new Parkinfo();
        $list = $Parkinfo->where($map)->limit($page, $query['limit'])->order('id desc')->select();
        $count = $Parkinfo->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        return $re_data;
    }

    public function parkadd($param)
    {
//        return json_encode($param);
        $cacheinfo=$this->getcacheinfo($param['token']);
        $query = json_decode($param['query'], true);
        $data['parktitle'] = $query['parktitle'];
        $data['parkno'] = $query['parkno'];
        $data['parkkey'] = $query['parkkey'];
        $data['micro_appid'] = $query['micro_appid'];
        $data['machid'] = $query['machid'];
        $data['parksize'] = $query['parksize'];
        $data['totalset'] = $query['totalset'];
        $data['is_open_wxwg'] = $query['is_open_wxwg'];
        $data['totalset'] = $query['totalset'];
        $data['create_time'] = time();
        $data['create_user'] = $cacheinfo['username'];
        $add = Parkinfo::create($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }

    public function parkedit($param)
    {
        $query = json_decode($param['query'], true);
        $data['id'] = $query['id'];
        $data['parktitle'] = $query['parktitle'];
        $data['parkno'] = $query['parkno'];
        $data['parkkey'] = $query['parkkey'];
        $data['micro_appid'] = $query['micro_appid'];
        $data['machid'] = $query['machid'];
        $data['parksize'] = $query['parksize'];
        $data['totalset'] = $query['totalset'];
        $data['is_open_wxwg'] = $query['is_open_wxwg'];
        $data['totalset'] = $query['totalset'];
        $add = Parkinfo::update($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }

    public function parkdel($param)
    {
        $query = json_decode($param['query'], true);
        $del = Parkinfo::destroy(['id' => $query['id']]);
        if ($del) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "删除失败";
        }
        return $re_data;
    }
    public function parkloglist($param)
    {
        $Parklog = new Parklog();
        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        $cacheinfo=$this->getcacheinfo($param['token']);
        if($cacheinfo['parkid']){
            $data['parkid']=$cacheinfo['parkid'];
        }
        if (isset($query['title'])) {
            $map['a.carno'] = array('like',"%".$query['carno']."%");
        }
        if (isset($query['sdate'])) {
            $map['a.intime'] = array('between',[$query['sdate'][0],$query['sdate'][1]]);
        }
        $list = $Parklog->alias('a')
            ->join('wn_parkinfo b', 'a.parkid=b.id')
            ->where($map)->limit($page, $query['limit'])->order('id desc')
            ->field('a.*,b.parktitle as parktitle')
            ->select();
        $sql=$Parklog->getLastSql();
        foreach($list as $k=>$v){
            $list[$k]['is_wx_wg']=$v['is_wx_wg']=="Y"?"是":"否";
            $list[$k]['status']=carstatus($v['status']);
            $list[$k]['paystatus']=getpaystatus($v['paystatus']);
        }
        $count = $Parklog->alias('a')->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        $re_data['data']['sql'] = $sql;
        return $re_data;
    }
    public function getparkloginfo($param){
        $query = json_decode($param['query'], true);
        $info=Parklog::get($query['id']);
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $info;
        return $re_data;
    }
    public function getparklanelist($param){
        $query = json_decode($param['query'], true);
        $page = ($query['page'] - 1) * $query['limit'];
        $map = [];
        if (isset($query['title'])) {
            $map['a.lane_title'] = array('like','%'.$query['title'].'%');
        }
        if (isset($query['parkid'])) {
            $map['a.parkid'] =$query['parkid'];
        }
        $Lane = new Lane();

        $list = $Lane->alias('a')
            ->join('wn_parkinfo b','a.parkid = b.id','left')
            ->where($map)->limit($page, $query['limit'])->order('a.id desc')
            ->field('a.*,b.parktitle as parktitle')->select();
        $count = $Lane->alias('a')->where($map)->count();
        $re_data['return_code'] = "SUCCESS";
        $re_data['return_msg'] = "成功";
        $re_data['data']['items'] = $list;
        $re_data['data']['total'] = $count;
        return $re_data;
    }
    public function parklaneadd($param)
    {
//        return json_encode($param);

        $query = json_decode($param['query'], true);
        $data['parkid'] = $query['parkid'];
        $data['lane_title'] = $query['lane_title'];
        $data['lane_num'] = $query['lane_num'];
        $data['lane_ip'] = $query['lane_ip'];
        $add = Lane::create($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }

    public function parklaneedit($param)
    {
        $query = json_decode($param['query'], true);
        $data['id'] = $query['id'];
        $data['parkid'] = $query['parkid'];
        $data['lane_title'] = $query['lane_title'];
        $data['lane_num'] = $query['lane_num'];
        $data['lane_ip'] = $query['lane_ip'];
        $add = Lane::update($data);
        if ($add) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        }
        return $re_data;
    }

    public function parklanedel($param)
    {
        $query = json_decode($param['query'], true);
        $del = Lane::destroy(['id' => $query['id']]);
        if ($del) {
            $re_data['return_code'] = "SUCCESS";
            $re_data['return_msg'] = "成功";
        } else {
            $re_data['return_code'] = "FAIL";
            $re_data['return_msg'] = "删除失败";
        }
        return $re_data;
    }

}